package com.dkd.manage.service.impl;

import java.util.List;
import com.dkd.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dkd.manage.mapper.StrategyMapper;
import com.dkd.manage.mapper.StrategyDeviceMapper;
import com.dkd.manage.domain.Strategy;
import com.dkd.manage.domain.StrategyDevice;
import com.dkd.manage.service.IStrategyService;

/**
 * 折扣策略Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-10
 */
@Service
public class StrategyServiceImpl implements IStrategyService
{
    @Autowired
    private StrategyMapper strategyMapper;

    @Autowired
    private StrategyDeviceMapper strategyDeviceMapper;

    /**
     * 查询折扣策略
     *
     * @param id 折扣策略主键
     * @return 折扣策略
     */
    @Override
    public Strategy selectStrategyById(Long id)
    {
        return strategyMapper.selectStrategyById(id);
    }

    /**
     * 查询折扣策略列表
     *
     * @param strategy 折扣策略
     * @return 折扣策略
     */
    @Override
    public List<Strategy> selectStrategyList(Strategy strategy)
    {
        return strategyMapper.selectStrategyList(strategy);
    }

    /**
     * 查询有效的策略列表
     *
     * @param strategy 策略条件
     * @return 有效策略列表
     */
    @Override
    public List<Strategy> selectActiveStrategyList(Strategy strategy)
    {
        return strategyMapper.selectActiveStrategyList(strategy);
    }

    /**
     * 新增折扣策略
     *
     * @param strategy 折扣策略
     * @return 结果
     */
    @Transactional
    @Override
    public int insertStrategy(Strategy strategy)
    {
        strategy.setCreateTime(DateUtils.getNowDate());
        int result = strategyMapper.insertStrategy(strategy);

        // 新增策略设备关联
        if (strategy.getStrategyDeviceList() != null && strategy.getStrategyDeviceList().size() > 0)
        {
            for (StrategyDevice strategyDevice : strategy.getStrategyDeviceList())
            {
                strategyDevice.setStrategyId(strategy.getId());
                strategyDevice.setAssignTime(DateUtils.getNowDate());
                strategyDevice.setCreateBy(strategy.getCreateBy());
                strategyDevice.setCreateTime(DateUtils.getNowDate());
            }
            strategyMapper.batchStrategyDevice(strategy.getStrategyDeviceList());
        }

        return result;
    }

    /**
     * 修改折扣策略
     *
     * @param strategy 折扣策略
     * @return 结果
     */
    @Transactional
    @Override
    public int updateStrategy(Strategy strategy)
    {
        strategy.setUpdateTime(DateUtils.getNowDate());

        // 先删除原有的策略设备关联
        strategyMapper.deleteStrategyDeviceByStrategyId(strategy.getId());

        // 新增策略设备关联
        if (strategy.getStrategyDeviceList() != null && strategy.getStrategyDeviceList().size() > 0)
        {
            for (StrategyDevice strategyDevice : strategy.getStrategyDeviceList())
            {
                strategyDevice.setStrategyId(strategy.getId());
                strategyDevice.setAssignTime(DateUtils.getNowDate());
                strategyDevice.setCreateBy(strategy.getUpdateBy());
                strategyDevice.setCreateTime(DateUtils.getNowDate());
            }
            strategyMapper.batchStrategyDevice(strategy.getStrategyDeviceList());
        }

        return strategyMapper.updateStrategy(strategy);
    }

    /**
     * 批量删除折扣策略
     *
     * @param ids 需要删除的折扣策略主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteStrategyByIds(Long[] ids)
    {
        // 删除策略设备关联
        strategyMapper.deleteStrategyDeviceByStrategyIds(ids);
        return strategyMapper.deleteStrategyByIds(ids);
    }

    /**
     * 删除折扣策略信息
     *
     * @param id 折扣策略主键
     * @return ���果
     */
    @Transactional
    @Override
    public int deleteStrategyById(Long id)
    {
        // 删除策略设备关联
        strategyMapper.deleteStrategyDeviceByStrategyId(id);
        return strategyMapper.deleteStrategyById(id);
    }

    /**
     * 分配策略给设备
     *
     * @param strategyId 策略ID
     * @param deviceIds 设备ID列表
     * @param createBy 创建者
     * @return 结果
     */
    @Override
    public int assignDevicesToStrategy(Long strategyId, Long[] deviceIds, String createBy)
    {
        return strategyDeviceMapper.batchAssignDevicesToStrategy(strategyId, deviceIds, createBy);
    }

    /**
     * 取消策略与设备的关联
     *
     * @param strategyId 策略ID
     * @param deviceIds 设备ID列表
     * @return 结果
     */
    @Override
    public int unassignDevicesFromStrategy(Long strategyId, Long[] deviceIds)
    {
        return strategyDeviceMapper.batchUnassignDevicesFromStrategy(strategyId, deviceIds);
    }

    /**
     * 根据设备ID查询适用的策略
     *
     * @param deviceId 设备ID
     * @return 策略列表
     */
    @Override
    public List<Strategy> selectStrategiesByDeviceId(Long deviceId)
    {
        List<StrategyDevice> strategyDevices = strategyDeviceMapper.selectStrategyDeviceByDeviceId(deviceId);
        if (strategyDevices == null || strategyDevices.isEmpty())
        {
            return null;
        }

        // 构建策略ID列表
        Long[] strategyIds = strategyDevices.stream()
                .map(StrategyDevice::getStrategyId)
                .toArray(Long[]::new);

        // 查询策略列表
        Strategy strategy = new Strategy();
        strategy.setStatus(1); // 只查询启用状态的策略
        List<Strategy> allStrategies = strategyMapper.selectActiveStrategyList(strategy);

        // 过滤出该设备关联的策略
        return allStrategies.stream()
                .filter(s -> contains(strategyIds, s.getId()))
                .collect(java.util.stream.Collectors.toList());
    }

    /**
     * 检查数组是否包含指定元素
     *
     * @param array 数组
     * @param value 元素值
     * @return 是否包含
     */
    private boolean contains(Long[] array, Long value)
    {
        if (array == null || array.length == 0)
        {
            return false;
        }
        for (Long item : array)
        {
            if (item.equals(value))
            {
                return true;
            }
        }
        return false;
    }
}